package com.ruoyi.system.dingutil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.Calendar;
import java.util.Date;

public class DingUtil {
	// 获取访问access_token
	public static final String ACCESS_TOKEN_URL = "https://oapi.dingtalk.com/gettoken?corpid=CORPID&corpsecret=CORPSECRET";
	
	public static final String JS_ACCESS_TOKEN_URL = "https://oapi.dingtalk.com/get_jsapi_ticket?access_token=ACCESS_TOKEN";
	
	public static final String GET_USER_INFO = "https://oapi.dingtalk.com/user/getuserinfo?access_token=ACCESS_TOKEN&code=AUTH_CODE";
	
	public static final String USER_GET = "https://oapi.dingtalk.com/user/get?access_token=ACCESS_TOKEN&userid=USER_ID";

	public static Date addTimes(int hours) {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.HOUR_OF_DAY, hours);
		return ca.getTime();
	}
	/**
	 * 获取访问令牌
	 * 
	 * @param appid
	 * @param appsecret
	 * @return
	 * @throws Exception
	 */
	public static JSONObject getAccessToken(String corpid, String corpsecret) throws Exception {
		JSONObject token = new JSONObject();
		token.put("errcode", -1);
		String url = ACCESS_TOKEN_URL.replace("CORPID", corpid).replace("CORPSECRET", corpsecret);
		JSONObject jsonObject = doGetStr(url);
		if (jsonObject != null) {
			int errcode = jsonObject.getIntValue("errcode");
			if (errcode == 0) {
				token.put("access_token", jsonObject.getString("access_token"));
				token.put("expires_in", jsonObject.getInteger("expires_in"));
				token.put("errcode", 0);
			}
		} else {
			token.put("errcode", -1);
		}
		return token;
	}
	
	public static JSONObject getJsAccessToken(String accessToken) throws Exception {
		JSONObject token = new JSONObject();
		token.put("errcode", -1);
		String url = JS_ACCESS_TOKEN_URL.replace("ACCESS_TOKEN", accessToken);
		JSONObject jsonObject = doGetStr(url);
		if (jsonObject != null) {
			int errcode = jsonObject.getIntValue("errcode");
			if (errcode == 0) {
				token.put("ticket", jsonObject.getString("ticket"));
				token.put("expires_in", jsonObject.getInteger("expires_in"));
				token.put("errcode", 0);
			}
		} else {
			token.put("errcode", -1);
		}
		return token;
	}
	public static JSONObject getUserInfo(String accessToken,String authCode){
		JSONObject info = new JSONObject();
		info.put("errcode", -1);
		String url = GET_USER_INFO.replace("ACCESS_TOKEN", accessToken).replace("AUTH_CODE", authCode);
		JSONObject jsonObject = doGetStr(url);
		if (jsonObject != null) {
			int errcode = jsonObject.getIntValue("errcode");
			if (errcode == 0) {
				info.put("userid", jsonObject.getString("userid"));
				info.put("errcode", 0);
			}else{
				info.put("errmsg", jsonObject.getString("errmsg"));
			}
		} else {
			info.put("errcode", -1);
		}
		return info;
	}
	public static JSONObject userGet(String accessToken,String userId){
		JSONObject info = new JSONObject();
		info.put("errcode", -1);
		String url = USER_GET.replace("ACCESS_TOKEN", accessToken).replace("USER_ID", userId);
		JSONObject jsonObject = doGetStr(url);
		if (jsonObject != null) {
			int errcode = jsonObject.getIntValue("errcode");
			if (errcode == 0) {
				info.put("infoData", jsonObject);
				info.put("errcode", 0);
			}else{
				info.put("errmsg", jsonObject.getString("errmsg"));
			}
		} else {
			info.put("errcode", -1);
		}
		return info;
	}


	/**
	 * get请求
	 * 
	 * @param url
	 * @return
	 * @throws ParseException
	 * @throws IOException
	 */
	public static JSONObject doGetStr(String url) {
		String result = NetUtils.get(url);
		return JSON.parseObject(result);
	}

}
